import java.awt.LayoutManager;
import java.awt.Rectangle;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
 * Create Date GUI allow the user to select the wanted date range.
 *
 */
public class BookReportDateGUI extends JPanel {

	private static final long serialVersionUID = 1L;
	public JComboBox jInYear = null;
	public JComboBox jInMon = null;
	public JComboBox jInDay = null;
	public JComboBox jOutYear = null;
	public JComboBox jOutMon = null;
	public JComboBox jOutDay = null;
	private JLabel jlblStart = null;
	private JLabel jlblEnd = null;
	public JButton jbtnBack1 = null;
	public JButton jbtnNext1 = null;
	/**
	 * save current date 
	 */
	String[] date;
	/**
	 * intDate save Start date.
	 * outDate save End date.
	 */
	int[] intDate,outDate;
	/**
	 * temporary int
	 */
	int i,d;
	public JLabel jlblTitle = null;
	/**
	 * inLeapYear - 1 if the start date year is a leap year
	 */
	int inLeapYear;
	/**
	 * outLeapYear - 1 if the end date year is a leap year
	 */
	int outLeapYear;
	/**
	 * Create and initializes  BookReportDateGUI.
	 */
	public BookReportDateGUI() {
		
		super();
		initialize();
	}

	public BookReportDateGUI(LayoutManager arg0) {
		super(arg0);
		// TODO Auto-generated constructor stub
		initialize();
	}

	public BookReportDateGUI(boolean arg0) {
		super(arg0);
		// TODO Auto-generated constructor stub
		initialize();
	}

	public BookReportDateGUI(LayoutManager arg0, boolean arg1) {
		super(arg0, arg1);
		// TODO Auto-generated constructor stub
		initialize();
	}

	/**
	 * initializes BookReportDateGUI
	 * 
	 * 
	 */
	private void initialize() {
		intDate=new int[3];
		outDate=new int[3];
		jlblTitle = new JLabel();
		jlblTitle.setBounds(new Rectangle(12, 17, 314, 43));
		jlblTitle.setText("Select Date for BookId:");
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
	    date=sdf.format(cal.getTime()).split("\\/");
		this.setSize(335, 232);
		this.setLayout(null);
		jlblStart = new JLabel();
		jlblStart.setBounds(new Rectangle(29, 74, 100, 20));
		jlblStart.setText("Start Date");
		jlblEnd = new JLabel();
		jlblEnd.setBounds(new Rectangle(29, 100, 100, 20));
		jlblEnd.setText("End Date");
		this.add(jlblStart, null);
		this.add(jlblEnd, null);
		this.add(getJInYear(), null);
		this.add(getJInMon(), null);
		this.add(getJInDay(), null);
		this.add(getJOutYear(), null);
		this.add(getJOutMon(), null);
		this.add(getJOutDay(), null);
		this.add(getJbtnBack(), null);
		this.add(getJbtnNext(), null);
		this.add(jlblTitle, null);
		lChangeDate();
		
	}

	/**
	 * initializes jInYear	
	 * 	
	 * @return jInYear JComboBox	
	 */
	private JButton getJbtnBack() {
		if (jbtnBack1 == null) {
			jbtnBack1 = new JButton();
			jbtnBack1.setBounds(new Rectangle(29, 135, 120, 29));
			jbtnBack1.setText("Back");
		}
		return jbtnBack1;
	}
	/**
	 * initializes getJbtnNext	
	 * 	
	 * @return getJbtnNext JButton	
	 */
	private JButton getJbtnNext() {
		if (jbtnNext1 == null) {
			jbtnNext1 = new JButton();
			jbtnNext1.setBounds(new Rectangle(155, 135, 120, 29));
			jbtnNext1.setText("Next>>");
		}
		return jbtnNext1;
	}
	/**
	 *initializes getJInYear,When item State Changed call to lChangeDate
	 * 	getJInYear allow user to select Start year
	 @return getJInYear JComboBox		
	 */
	private JComboBox getJInYear() {
		if (jInYear == null) {
			jInYear = new JComboBox();
			jInYear.setBounds(new Rectangle(252, 75, 55, 20));
			for(i=-10;i<10;i++)
			{
				d=Integer.parseInt(date[2])+i;
				if(d<10)
					jInYear.addItem("200"+d);
				else
					jInYear.addItem("20"+d);
			}
			jInYear.setSelectedIndex(Integer.parseInt(date[2])-1);
			jInYear.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					lChangeDate();
				}
			});
		}
		return jInYear;
	}
	/**
	 * initializes getJOutYear	
	 * 	getJOutYear allow user to select End year
	 * JComboBox,When item State Changed call to lChangeDate
	 * @return getJOutYear JComboBox		
	 */
	private JComboBox getJOutYear() {
		if (jOutYear == null) {
			jOutYear = new JComboBox();
			jOutYear.setBounds(new Rectangle(250, 102, 55, 20));
			for(i=-10;i<10;i++)
			{
				d=Integer.parseInt(date[2])+i;
				if(d<10)
					jOutYear.addItem("200"+d);
				else
					jOutYear.addItem("20"+d);
			}
			jOutYear.setSelectedIndex(Integer.parseInt(date[2])-1);
			jOutYear.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					lChangeDate();
				}
			});
		}
		return jOutYear;
	}

	/**
	 * initializes jInMon,When item State Changed call to lChangeDate	
	 * 	jInMon allow user to select Start month  	
	 * @return jInMon JComboBox	
	 */
	private JComboBox getJInMon() {
		if (jInMon == null) {
			jInMon = new JComboBox();
			jInMon.setBounds(new Rectangle(190, 74, 55, 20));
			for(i=1;i<13;i++)
				jInMon.addItem(i);
			jInMon.setSelectedIndex(Integer.parseInt(date[0])-1);
			jInMon.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					lChangeDate();
				}
			});
		}
		return jInMon;
	}
	/**
	 *initializes getJOutMon,When item State Changed call to lChangeDate	
	 * 	getJOutMon allow user to select End month  
	 * @return getJOutMon JComboBox		
	 */
	private JComboBox getJOutMon() {
		if (jOutMon == null) {
			jOutMon = new JComboBox();
			jOutMon.setBounds(new Rectangle(190, 100, 55, 20));
			for(i=1;i<13;i++)
				jOutMon.addItem(i);
			jOutMon.setSelectedIndex(Integer.parseInt(date[0])-1);
			jOutMon.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					lChangeDate();
				}
			});
		}
		return jOutMon;
	}
	/**
	 * initializes jInDay	
	 * jInDay allow user to select Start Day  	
	 * @return jInDay JComboBox		
	 */
	private JComboBox getJInDay() {
		if (jInDay == null) {
			jInDay = new JComboBox();
			jInDay.setBounds(new Rectangle(132, 74, 55, 20));
			for(i=1;i<32;i++)
				jInDay.addItem(i);
			jInDay.setSelectedIndex(Integer.parseInt(date[1])-1);
		}
		return jInDay;
	}
	/**
	 * initializes JOutDay	
	 * JOutDay allow user to select End Day 	
	 * @return JOutDay JComboBox			
	 */
	private JComboBox getJOutDay() {
		if (jOutDay == null) {
			jOutDay = new JComboBox();
			jOutDay.setBounds(new Rectangle(131, 100, 55, 20));
			for(i=1;i<32;i++)
				jOutDay.addItem(i);
			jOutDay.setSelectedIndex(Integer.parseInt(date[1]));
		}
		return jOutDay;
	}
	/**
	 * Receive for GUI the year and month for the start date and end date, and calculate the number of days in these months,finally it changes jOutDay and jInDay to these numbers.
	 */
	public void lChangeDate()
	{
		int inTempYear,outTempYear,inTempMonth ,outTempMonth;
		inTempYear=Integer.parseInt((String)jInYear.getSelectedItem());
		outTempYear=Integer.parseInt((String)jOutYear.getSelectedItem());
		inTempMonth=jInMon.getSelectedIndex()+1;
		outTempMonth=jOutMon.getSelectedIndex()+1;
	if(inTempYear%400==0)
	       inLeapYear=1;
	else if (inTempYear%100==0)
		 inLeapYear=0;
	else if (inTempYear%4==0)
		inLeapYear=1;
	else
		inLeapYear=0;
	if(outTempYear%400==0)
	       outLeapYear=1;
	else if (outTempYear%100==0)
		outLeapYear=0;
	else if (outTempYear%4==0)
		outLeapYear=1;
	else
		outLeapYear=0;
	jInDay.removeAllItems();
	if(inTempMonth%2==0)
	{
		if(inTempMonth==2)
		{
			if(inLeapYear==1)
			{
				for(i=0;i<29;i++)
					jInDay.addItem(i+1);
			}
			else
			{
				for(i=0;i<28;i++)
					jInDay.addItem(i+1);
			}
		}
		else
		{
			for(i=0;i<30;i++)
				jInDay.addItem(i+1);
		}
	}
	else
	{
		for(i=0;i<31;i++)
			jInDay.addItem(i+1);
	}
	jOutDay.removeAllItems();
	if(outTempMonth%2==0)
	{
		if(outTempMonth==2)
		{
			if(outLeapYear==1)
			{
				for(i=0;i<29;i++)
					jOutDay.addItem(i+1);
			}
			else
			{
				for(i=0;i<28;i++)
					jOutDay.addItem(i+1);
			}
		}
		else
		{
			for(i=0;i<30;i++)
				jOutDay.addItem(i+1);
		}
	}
	else
	{
		for(i=0;i<31;i++)
			jOutDay.addItem(i+1);
	}
		
	}
}  
